/**
 * 
 */
package org.database.filter.ws;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.database.filter.model.Excel;
import org.database.filter.model.NhaDauTuDoanhNghiep;
import org.database.filter.model.TuNhan;
import org.database.filter.service.ExcelService;
import org.database.filter.service.TextFilterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("databaseFilter")
@Path("/filter")
public class GameWebService {
	@Autowired
	private TextFilterService textFilterService;

	@Autowired
	private ExcelService excelService;

	@GET
	@Path("/run")
	public String run() {
		doFilter();
		return "done";
	}

	private void doFilter() {
		// lấy các dòng trong bảng excel
		List<Excel> excelList = excelService.getAll();
		for (Excel excel : excelList) {
			try {
				// chuan hoa Loai Ho So
				excel.setLOAI_HS(textFilterService.doFilterLoaiHS(excel
						.getLOAI_HS()));
				// chuan hoa Ma Tinh Trang
				excel.setMA_TT(filterLoaiTinhTrang(excel));
				
				// chuan hoa Hinh Thuc Dau Tu
				excel.setHINH_THUC_DAU_TU(textFilterService
						.doFilterHinhThuc(excel.getHINH_THUC_DAU_TU()));
				
				// chuan hoa Nganh Nghe
				excel = filterNganh(excel);

				// chuan hoa muc tieu
				excel = filterMuctieu(excel);
				
				//chuan hoa Nha Dau Tu
				excel = filterNhaDauTu(excel);
				
				// đưa các dữ liệu là khoảng trắng hoặc chữ null về giá trị NULL
				
				excel.setBAT_DONG_SAN(setNull(excel.getBAT_DONG_SAN()));
				excel.setCAC_BEN_GOP(setNull(excel.getCAC_BEN_GOP()));
				excel.setCAC_BEN_GOP_DA(setNull(excel.getCAC_BEN_GOP_DA()));
				excel.setCAP(setNull(excel.getCAP()));
				excel.setCHUC_DANH(setNull(excel.getCHUC_DANH()));
				excel.setDAI_DIEN_PL(setNull(excel.getDAI_DIEN_PL()));
				excel.setDIA_CHI(setNull(excel.getDIA_CHI()));
				excel.setDIA_CHI_CN(setNull(excel.getDIA_CHI_CN()));
				excel.setDIA_CHI_DA(setNull(excel.getDIA_CHI_DA()));

				excel.setDIA_CHI_NDTNN(setNull(excel.getDIA_CHI_NDTNN()));
				excel.setDIA_CHI_TRU_SO_CHINH(setNull(excel
						.getDIA_CHI_TRU_SO_CHINH()));
				excel.setDIEN_THOAI(setNull(excel.getDIEN_THOAI()));
				excel.setDIEN_THOAI_CN(setNull(excel.getDIEN_THOAI_CN()));
				excel.setDIEN_TICH_DAT_SU_DUNG(setNull(excel
						.getDIEN_TICH_DAT_SU_DUNG()));
				excel.setDNCX(setNull(excel.getDNCX()));
				excel.setFAX(setNull(excel.getFAX()));
				excel.setFAX_CN(setNull(excel.getFAX_CN()));
				excel.setGHI_CHU_DC(setNull(excel.getGHI_CHU_DC()));
				excel.setGIAY_PHEP_KD(setNull(excel.getGIAY_PHEP_KD()));
				excel.setDIEN_THOAI(setNull(excel.getDIEN_THOAI()));
				excel.setKCN(setNull(excel.getKCN()));
				excel.setLINH_VUC(setNull(excel.getLINH_VUC()));
				excel.setHUYEN(textFilterService.doFilterHuyen(excel.getHUYEN()));
				excel.setLOAI_HINH_DN(textFilterService
						.doFilterLoaiHinhDoanhNghiep(excel.getLOAI_HINH_DN()));

				excelService.saveOrUpdate(excel);
			} catch (Exception exc) {
				System.out.println("ERROR IN UPDATE" + exc.getMessage());
			}
		}

	}

	private int filterLoaiTinhTrang(Excel excel) {
		int matt = 1;
		try {
			matt = Integer.parseInt(textFilterService
					.doFilterTinhTrang(excel.getTINH_TRANG()));
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return matt;
	}
	
	private Excel filterNganh(Excel excel) {
		// tách ngành thành các ngành
		List<Long> list = textFilterService.doFilterNganhNghe(
				excel.getNGANH());
		
		// đưa danh sách ngành vào các ngành
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				switch (i) {
				case 0:
					excel.setNGANH_1(list.get(i));
					break;
				case 1:
					excel.setNGANH_2(list.get(i));
					break;
				case 2:
					excel.setNGANH_3(list.get(i));
					break;
				case 3:
					excel.setNGANH_4(list.get(i));
					break;
				case 4:
					excel.setNGANH_5(list.get(i));
					break;
				case 5:
					excel.setNGANH_6(list.get(i));
					break;
				case 6:
					excel.setNGANH_7(list.get(i));
					break;
				case 7:
					excel.setNGANH_8(list.get(i));
					break;
				case 8:
					excel.setNGANH_9(list.get(i));
					break;
				case 9:
					excel.setNGANH_10(list.get(i));
					break;
				default:
					break;
				}
			}
		}
		return excel;
	}
	
	private Excel filterMuctieu(Excel excel) {
		// tách mục tiêu thành các mục tiêu
		List<Integer> listMT = textFilterService.doFilterMucTieu(
				excel.getMUC_TIEU_HOAT_DONG());
		
		// đưa danh sách mục tiêu vào từng mục tiêu trong bảng excel
		if (listMT != null) {
			for (int i = 0; i < listMT.size(); i++) {
				switch (i) {
				case 0:
					excel.setMUC_TIEU_1(listMT.get(i));
					break;
				case 1:
					excel.setMUC_TIEU_2(listMT.get(i));
					break;
				case 2:
					excel.setMUC_TIEU_3(listMT.get(i));
					break;
				case 3:
					excel.setMUC_TIEU_4(listMT.get(i));
					break;
				case 4:
					excel.setMUC_TIEU_5(listMT.get(i));
					break;
				case 5:
					excel.setMUC_TIEU_6(listMT.get(i));
					break;
				case 6:
					excel.setMUC_TIEU_7(listMT.get(i));
					break;
				default:
					break;
				}
			}
		}
		return excel;
	}
	
	private Excel filterNhaDauTu(Excel excel) {
		List<TuNhan> lstNhaDT = new ArrayList<TuNhan>();
		List<NhaDauTuDoanhNghiep> lstNhaDTDN = new ArrayList<NhaDauTuDoanhNghiep>();
		Integer ma_qg = excel.getMA_QG();
		// kiểm tra Cột TEN_NDT là doanh nghiệp hay tư nhân
		if (!textFilterService.isDoanhNghiep(excel.getTEN_NDT())) {
			// tách danh sách các Tư Nhân
			List<TuNhan> list = textFilterService.doFilterDauTuCaNhan(
					excel.getTEN_NDT(), ma_qg, excel.getDIA_CHI());
			if (list != null) {
				lstNhaDT.addAll(list);
			}
		} else {
			// tách danh sách các Doanh Nghiệp
			List<NhaDauTuDoanhNghiep> list = textFilterService
					.doFilterDauTuDoanhNghiep(excel.getTEN_NDT(), ma_qg);
			if (list != null) {
				lstNhaDTDN.addAll(list);
			}
		}
		
		// kiểm tra Cột TEN_NDT_NN là doanh nghiệp hay tư nhân
		if (!textFilterService.isDoanhNghiep(excel.getTEN_NDT_NN())) {
			// tách danh sách các Tư Nhân
			List<TuNhan> list = textFilterService.doFilterDauTuCaNhan(
					excel.getTEN_NDT_NN(), ma_qg,
					excel.getDIA_CHI_NDTNN());
			if (list != null) {
				lstNhaDT.addAll(list);
			}
		} else {
			// tách danh sách các Doanh Nghiệp
			List<NhaDauTuDoanhNghiep> list = textFilterService
					.doFilterDauTuDoanhNghiep(excel.getTEN_NDT_NN(),
							ma_qg);
			if (list != null) {
				lstNhaDTDN.addAll(list);
			}
		}
		// đua danh sách tư nhân vào cột tư nhân
		if (lstNhaDT != null) {
			for (int i = 0; i < lstNhaDT.size(); i++) {
				TuNhan ndt = lstNhaDT.get(i);
				Integer maQG = null;
				try {
					maQG = Integer.parseInt(ndt.getHO_KHAU());
				} catch (Exception exx) {
					exx.printStackTrace();
				}
				switch (i) {
				case 0:
					excel.setNHA_DAU_TU_1(ndt.getCMND_PASSPORT());
					excel.setMA_QG_NDT_1(maQG);
					break;
				case 1:
					excel.setNHA_DAU_TU_2(ndt.getCMND_PASSPORT());
					excel.setMA_QG_NDT_2(maQG);
					break;
				case 2:
					excel.setNHA_DAU_TU_3(ndt.getCMND_PASSPORT());
					excel.setMA_QG_NDT_3(maQG);
					break;
				case 3:
					excel.setNHA_DAU_TU_4(ndt.getCMND_PASSPORT());
					excel.setMA_QG_NDT_4(maQG);
					break;
				case 4:
					excel.setNHA_DAU_TU_5(ndt.getCMND_PASSPORT());
					excel.setMA_QG_NDT_5(maQG);
					break;
				case 5:
					excel.setNHA_DAU_TU_6(ndt.getCMND_PASSPORT());
					excel.setMA_QG_NDT_6(maQG);
					break;
				case 6:
					excel.setNHA_DAU_TU_7(ndt.getCMND_PASSPORT());
					excel.setMA_QG_NDT_7(maQG);
					break;
				case 7:
					excel.setNHA_DAU_TU_8(ndt.getCMND_PASSPORT());
					excel.setMA_QG_NDT_8(maQG);
					break;

				default:
					break;
				}
			}
		}

		// đua danh sách Doanh Nghiep vào cột Doanh Nghiep
		if (lstNhaDTDN != null) {
			for (int i = 0; i < lstNhaDTDN.size(); i++) {
				NhaDauTuDoanhNghiep ndt = lstNhaDTDN.get(i);
				switch (i) {
				case 0:
					excel.setNHA_DAU_TU_DN_1(ndt.getID_DN());
					excel.setMA_QG_NDT_DN_1(ma_qg);
					break;
				case 1:
					excel.setNHA_DAU_TU_DN_2(ndt.getID_DN());
					excel.setMA_QG_NDT_DN_2(ma_qg);
					break;
				case 2:
					excel.setNHA_DAU_TU_DN_3(ndt.getID_DN());
					excel.setMA_QG_NDT_DN_3(ma_qg);
					break;
				case 3:
					excel.setNHA_DAU_TU_DN_4(ndt.getID_DN());
					excel.setMA_QG_NDT_DN_4(ma_qg);
					break;
				case 4:
					excel.setNHA_DAU_TU_DN_5(ndt.getID_DN());
					excel.setMA_QG_NDT_DN_5(ma_qg);
					break;
				case 5:
					excel.setNHA_DAU_TU_DN_6(ndt.getID_DN());
					excel.setMA_QG_NDT_DN_6(ma_qg);
					break;
				case 6:
					excel.setNHA_DAU_TU_DN_7(ndt.getID_DN());
					excel.setMA_QG_NDT_DN_7(ma_qg);
					break;
				case 7:
					excel.setNHA_DAU_TU_DN_8(ndt.getID_DN());
					excel.setMA_QG_NDT_DN_8(ma_qg);
					break;
				default:
					break;
				}
			}
		}
		return excel;
	}
	
	private String setNull(String input) {
		if (input == null || input.trim().isEmpty()) {
			return null;
		}
		return input;
	}

	/*private boolean isVsip(String macq) {
		return macq.contains("VSIP");
	}*/
}
